
package com.hlkj.pay.service.common.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.common.OperatedLogPageDto;
import com.hlkj.pay.dto.common.OperatedLogPageQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.mapper.common.OperatedLogMapper;
import com.hlkj.pay.infrastructure.model.common.OperatedLogDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.common.IOperatedLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/09/25 13:33
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class OperatedLogServiceImpl extends CommonSnFilterService implements IOperatedLogService {

    private final OperatedLogMapper operatedLogMapper;

    @Override
    public PageResult<OperatedLogPageDto> queryPage(OperatedLogPageQueryDto operatedLogPageQueryDto) {
        if (!StringUtils.hasText(operatedLogPageQueryDto.getSortingFields())) {
            operatedLogPageQueryDto.setSortingFields("id");
        }
        IPage<OperatedLogPageQueryDto> mpPage = MyBatisUtils.buildPage(operatedLogPageQueryDto);
        QueryWrapper<OperatedLogDO> queryWrapper = buildInfoWrapper(operatedLogPageQueryDto);
//        queryWrapper.orderByDesc("a.id");
        operatedLogMapper.selectOperatedLogPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    @Override
    public Long save(Long relationId, MerchantEnum.MERCHANT_LOG_TYPE relationType, String operType, String remark) {
        OperatedLogDO operatedLogDO = new OperatedLogDO();
        operatedLogDO.setRelationType(relationType.getCode());
        operatedLogDO.setRelationId(relationId);
        operatedLogDO.setRemark(remark);
        operatedLogDO.setOperType(operType);
        operatedLogMapper.insert(operatedLogDO);
        return null;
    }

    /**
     * 查询条件封装
     * 
     * @param operatedLogPageQueryDto
     * @return
     */
    QueryWrapper<OperatedLogDO> buildInfoWrapper(OperatedLogPageQueryDto operatedLogPageQueryDto) {
//        filterSn(merchantAccessLogPageQueryDto);
        QueryWrapperX<OperatedLogDO> queryWrapper = new QueryWrapperX<>();
        queryWrapper.eq("a.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eqIfPresent("a.relation_type", operatedLogPageQueryDto.getRelationType());
        queryWrapper.eqIfPresent("a.relation_id", operatedLogPageQueryDto.getRelationId());
        queryWrapper.eqIfPresent("a.oper_type", operatedLogPageQueryDto.getOperType());
        return queryWrapper;
    }

}
